package com.hesicare.health.service.impl;

import com.hesicare.adapter.worker.HisWorkManagers;
import com.hesicare.cgm.entity.CgmPatientDTO;
import com.hesicare.health.entity.SysPatient;
import com.hesicare.health.mapper.SysPatientMapper;
import com.hesicare.health.service.ISysPatientRecordService;
import com.hesicare.health.service.ISysPatientService;
import com.hesicare.untils.StringUtil.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hesicare.untils.exceptionUtils.exceptionutil.errorTrackSpace;

/**
 * @author 55097
 */
@Service
public class SysPatientServiceImpl implements ISysPatientService {

    protected final static Logger log = LoggerFactory.getLogger(HisWorkManagers.class);

    @Autowired
    private SysPatientMapper sysPatientMapper;

    @Autowired
    private ISysPatientRecordService ISysPatientRecordService;

    @Override
    public void insertSysPatient(SysPatient sysPatient) {
        if (sysPatient != null) {
            sysPatientMapper.insertSysPatient(sysPatient);
        }
    }

    @Override
    public void updateSysPatient(SysPatient sysPatient) {
        if (sysPatient != null) {
            sysPatientMapper.updateSysPatient(sysPatient);
        }

    }

    @Override
    public SysPatient selectSysPatientByHospitalizedNum(String hospitalizedNum) {
        SysPatient sysPatient = null;
        if (StringUtil.isNotBlank(hospitalizedNum)) {
            sysPatient = sysPatientMapper.selectSysPatientByHospitalizedNum(hospitalizedNum);
        }
        return sysPatient;
    }

    @Override
    public SysPatient selectSysPatientByHisPatientId(String hisPaitentId) {
        SysPatient sysPatient = null;
        if (StringUtil.isNotBlank(hisPaitentId)) {
            sysPatient = sysPatientMapper.selectSysPatientByHisPatientId(hisPaitentId);
        }
        return sysPatient;
    }

    @Override
    public List<String> selectHisPatientIdByHospitalType() {
        return sysPatientMapper.selectHisPatientIdByHospitalType();
    }

    @Override
    public List<SysPatient> selectHospitalNumByHospitalType() {
        return sysPatientMapper.selectHospitalNumByHospitalType();
    }

    @Override
    public List<CgmPatientDTO> selectSysPatientBybindingStatus() {
        return sysPatientMapper.selectSysPatientBybindingStatus();
    }

    @Override
    public void updateHisPatientByhospitalizedType(List<String> patientIds) {
        if (!patientIds.isEmpty()) {
            try {
                ISysPatientRecordService.insertPtientRecordByPatientList(patientIds);
            } catch (Exception e) {
                log.error("新增出院患者错误", errorTrackSpace(e));
            }
            try {
                sysPatientMapper.updateHisPatientByhospitalizedType(patientIds);
            } catch (Exception e) {
                log.error("更新患者状态错误", errorTrackSpace(e));
            }
            log.info("出院患者{}", patientIds.toString());
        }
    }
}
